package com.wejoy.video.middle.station.data.service.file.impl;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import com.wejoy.video.middle.station.data.enums.file.EnumUpload;
import com.wejoy.video.middle.station.data.exception.BusinessException;
import com.wejoy.video.middle.station.data.service.file.IFileService;
import com.wejoy.video.middle.station.data.util.UuidUtils;
import com.wejoy.video.middle.station.data.vo.file.FileResLibVO;
import com.wejoy.video.middle.station.data.vo.file.UploadFileVO;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.attribute.PosixFilePermission;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;

//@Profile("dev")
@Component
public class LocalFileServiceImpl implements IFileService {

    @Value("${localFilePath}")
    private String localFilePath;

//    @Override
//    public UploadFileVO upload(EnumUpload enumUpload, byte[] bytes) {
//        String baseDir = enumUpload.getBaseDir();
//        String date = DateUtil.format(new Date(), "yyMMdd");
//        String folderPath = localFilePath + baseDir + "/" + date + "/";
//        String fileName = UuidUtils.uuid() + "." + enumUpload.getSuffix();
//        String key = baseDir + "/" + date + "/" + fileName;
//        File folder = new File(folderPath);
//        if (!folder.exists()) {
//            //递归生成文件夹
//            if (!folder.mkdirs()) {
//                throw new BusinessException("生成文件夹失败，请联系管理员排查");
//            }
//        }
//        FileUtil.writeBytes(bytes,new File(folderPath + fileName));
//        //设置权限
//        Set<PosixFilePermission> perms = new HashSet<>();
//        perms.add(PosixFilePermission.OWNER_READ);
//        perms.add(PosixFilePermission.OWNER_WRITE);
//        perms.add(PosixFilePermission.GROUP_READ);
//        perms.add(PosixFilePermission.OTHERS_READ);
//        try {
//            Files.setPosixFilePermissions(Paths.get(folderPath + fileName),perms);
//        } catch (IOException e) {
//            throw new BusinessException("上传文件报错：",e);
//        }
//        UploadFileVO res = new UploadFileVO();
//        res.setKey(key);
//        return res;
//    }

    @Override
    public UploadFileVO upload(EnumUpload enumUpload, byte[] bytes) {
        String baseDir = enumUpload.getBaseDir();
        String date = DateUtil.format(new Date(), "yyMMdd");
        String folderPath = localFilePath + baseDir + "/" + date + "/";
        String fileName = UUID.randomUUID().toString() + "." + enumUpload.getSuffix();
        String key = baseDir + "/" + date + "/" + fileName;
        File folder = new File(folderPath);
        if (!folder.exists()) {
            // Create folder if it doesn't exist
            if (!folder.mkdirs()) {
                throw new BusinessException("Failed to create folder, please contact the administrator.");
            }
        }
        // Write bytes to file
        try {
            Files.write(Paths.get(folderPath + fileName), bytes);
        } catch (IOException e) {
            throw new BusinessException("Error uploading file: ", e);
        }
        UploadFileVO res = new UploadFileVO();
        res.setKey(key);
        return res;
    }


    @Override
    public String uploadFileByImgUrl(String url, String suffix) {
        return null;
    }

    @Override
    public String getUploadToken() {
        return null;
    }

    @Override
    public String getUploadToken(String filePath) {
        return null;
    }

    @Override
    public FileResLibVO getVideoByVideoPath(String videoPath) {
        return null;
    }

    @Override
    public void refreshCDN(String path) {

    }

    @Override
    public FileResLibVO getImageByImagePath(String imagePath) {
        return null;
    }

}

